package com.hrsystem.controller;


import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hrsystem.VO.NewSalaryList;
import com.hrsystem.entity.*;
import com.hrsystem.lang.Result;
import com.hrsystem.mapper.OrgThirdMapper;
import com.hrsystem.service.OrgThirdService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


@RestController
public class OrgThirdController {

    @Resource
    OrgThirdMapper orgThirdMapper;

    @Autowired
    OrgThirdService orgThirdService;

    @Autowired
    SalaryListController salaryListController;

    @Transactional
    @GetMapping("/selectThirdOrg/{id}")
    public Result selectThirdOrgList(@PathVariable(name = "id") Long id){
        QueryWrapper<OrgThird> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sOrg_id",id);
        List<OrgThird> orgThirdList = orgThirdMapper.selectList(queryWrapper);
        return Result.success(orgThirdList);
    }

    @Transactional
    @PostMapping("/AddThirdOrg")
    public Result addFirstOrg(@RequestBody NewSalaryList newSalaryList) {
        OrgThird orgThird;
        orgThird = new OrgThird();
        BeanUtils.copyProperties(newSalaryList,orgThird);
        orgThirdService.saveOrUpdate(orgThird);
        QueryWrapper<OrgThird> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sOrg_id",orgThird.getSorgId());
        queryWrapper.eq("tOrg_name", orgThird.getTorgName());
        OrgThird orgThirdNew = orgThirdMapper.selectOne(queryWrapper);
        SalaryList salaryList;
        salaryList = new SalaryList();
        BeanUtils.copyProperties(newSalaryList,salaryList);
        BeanUtils.copyProperties(orgThirdNew,salaryList);
        salaryListController.addSalaryNewList(salaryList);
        return Result.success(null);
    }

}
